export default {
  data() {
    return {
      init_data: {},
    }
  },
  methods: {
    async getDictList(type) {
			let list = [];
			let initData = this.$store.getters.initData || {};
			if (JSON.stringify(initData) != '{}') {
			  if (initData.hasOwnProperty(type + '_list')) {
				list = initData[type + '_list'];
			  } else {
				let res = await this.$request('get', '/admin/dict/type/' + type, {});
				if (res.length > 0) {
				  list = this.$util.transformArray(res);
				  initData[type + '_list'] = list;
				  this.$store.commit('setInitData', initData);
				}
			  }
			} else {
				let res = await this.$request('get', '/admin/dict/type/' + type, {});
				if (res.length > 0) {
				  list = this.$util.transformArray(res);
				  initData[type + '_list'] = list;
				  this.$store.commit('setInitData', initData);
				}
			}
			return new Promise((resolve, reject) => {
				resolve(list);
			});
		},
	// async getDictList(type) {
	// 	let list = [];
	// 	let initData = sessionStorage.getItem('initData') || {};
	// 	if (JSON.stringify(initData) != '{}') {
	// 	  initData = JSON.parse(initData);
	// 	  if (initData.hasOwnProperty(type + '_list')) {
	// 		list = initData[type + '_list'];
	// 	  } else {
	// 		let res = await this.$request('get', '/admin/dict/type/' + type, {});
	// 		if (res.length > 0) {
	// 		  list = this.$util.transformArray(res);
	// 		  initData[type + '_list'] = list;
	// 		  sessionStorage.setItem('initData', JSON.stringify(initData))
	// 		}
	// 	  }
	// 	} else {
	// 	  let res = await this.$request('get', '/admin/dict/type/' + type, {});
	// 	  if (res.length > 0) {
	// 		list = this.$util.transformArray(res);
	// 		initData[type + '_list'] = list;
	// 		sessionStorage.setItem('initData', JSON.stringify(initData))
	// 	  }
	// 	}
	// 	return new Promise((resolve, reject) => {
	// 	  resolve(list);
	// 	});
	//   },
    setName(id, type) {
      let type_name = type.indexOf('_list') == -1 ? type + '_list' : type;
      let list = this.init_data[type_name];
      if (id != null) {
        let name;
        let arr = list.filter(item => item.id == id);
        if (arr.length == 1) {
          name = arr[0].name;
        } else {
          name = '';
        }
        return name;
      } else {
        return '';
      }
    },
  }
}
